
* Table 106 
qui{
clear 
use "Experiments Stata Files/Experiment0.dta"
gen exp=0
table latent_health if age==25, stat(fvpercent education)
table latent_health if age<65, stat(fvpercent education)


sort experiment ID age
gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort exp ID: egen earnings_PV = total(discounted_earnings)

bysort exp ID: egen age_death=max(age)
replace age_death=age_death+1 
gen age_death_65=age_death
replace age_death_65=65 if age_death_65>65
replace age_death_65=age_death_65-65
replace age_death_65=age_death_65*(-1)


* here we do health shocks not interacted with treatment 

gen combo_orig0= 0 if age<65
replace combo_orig0=1 if du==0 & dp==0 & s==0 & age<65

gen combo_orig1= 0 if age<65
replace combo_orig1=1 if du==1 & dp==0 & s==0  & age<65

gen combo_orig2= 0 if age<65
replace combo_orig2=1 if du==0 & dp==1 & s==0  & age<65

gen combo_orig3= 0 if age<65
replace combo_orig3=1 if du==0 & dp==0 & s==1  & age<65

gen combo_orig4= 0 if age<65
replace combo_orig4=1 if du==1 & dp==1 & s==0  & age<65

gen combo_orig5= 0 if age<65
replace combo_orig5=1 if du==1 & dp==0 & s==1  & age<65

gen combo_orig6= 0 if age<65
replace combo_orig6=1 if du==0 & dp==1 & s==1  & age<65

gen combo_orig7= 0 if age<65
replace combo_orig7=1 if du==1 & dp==1 & s==1  & age<65


* version interacted with treatment
* number of times you had each combination of shock
gen combo0= 0 if age<65
replace combo0=1 if du==0 & dp==0 & s==0 & age<65

gen combo1= 0 if age<65
replace combo1=1 if du==1 & dp==0 & s==0 & I_treat==0 & age<65

gen combo2= 0 if age<65
replace combo2=1 if du==0 & dp==1 & s==0 & I_treat==0 & age<65

gen combo3= 0 if age<65
replace combo3=1 if du==0 & dp==0 & s==1 & I_treat==0 & age<65

gen combo4= 0 if age<65
replace combo4=1 if du==1 & dp==1 & s==0 & I_treat==0 & age<65

gen combo5= 0 if age<65
replace combo5=1 if du==1 & dp==0 & s==1 & I_treat==0 & age<65

gen combo6= 0 if age<65
replace combo6=1 if du==0 & dp==1 & s==1 & I_treat==0 & age<65

gen combo7= 0 if age<65
replace combo7=1 if du==1 & dp==1 & s==1 & I_treat==0 & age<65

gen combo8= 0 if age<65
replace combo8=1 if du==1 & dp==0 & s==0 & I_treat==1 & age<65

gen combo9= 0 if age<65
replace combo9=1 if du==0 & dp==1 & s==0 & I_treat==1 & age<65

gen combo10= 0 if age<65
replace combo10=1 if du==0 & dp==0 & s==1 & I_treat==1 & age<65

gen combo11= 0 if age<65
replace combo11=1 if du==1 & dp==1 & s==0 & I_treat==1 & age<65

gen combo12= 0 if age<65
replace combo12=1 if du==1 & dp==0 & s==1 & I_treat==1 & age<65

gen combo13= 0 if age<65
replace combo13=1 if du==0 & dp==1 & s==1 & I_treat==1 & age<65

gen combo14= 0 if age<65
replace combo14=1 if du==1 & dp==1 & s==1 & I_treat==1 & age<65

foreach num of numlist 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 { 
by  exp ID: egen Total_combo`num'_65 = total(combo`num') if age<65
}

foreach num of numlist 0 1 2 3 4 5 6 7  { 
by  exp ID: egen Total_combo_orig`num'_65 = total(combo_orig`num') if age<65
}

****  generate a variable capturing the age at which the first dp and du shock occurred. 
gen age_dp=age if dp==1
gen age_du=age if du==1
by  exp ID: egen age_dp_min = min(age_dp)
by  exp ID: egen age_du_min = min(age_du)
replace age_dp_min =100 if age_dp_min==. // never had a shock
replace age_du_min =100 if age_du_min==. // never had a shock
* we want a variable with the min age only up to 65
gen age_du_min_65=65
gen age_dp_min_65=65
replace age_du_min_65=age_du_min if age_du_min<65
replace age_dp_min_65=age_dp_min if age_dp_min<65

*** create variables with the number of times the health shocks occur in Good, Fair and Poor health. 
*** each combo will be split in 3. 
foreach num of numlist 0 1 2 3 4 5 6 7 {
gen H1_combo_orig`num' = combo_orig`num' if H==1 // poor health
gen H2_combo_orig`num' = combo_orig`num' if H==2 // fair health
gen H3_combo_orig`num' = combo_orig`num' if H==3 // good health

}

foreach num of numlist 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14{
gen H1_combo`num' = combo`num' if H==1 // poor health
gen H2_combo`num' = combo`num' if H==2 // fair health
gen H3_combo`num' = combo`num' if H==3 // good health

}

foreach num of numlist 0 1 2 3 4 5 6 7{
by  exp ID: egen Total_H1_combo_orig`num'_65 = total(H1_combo_orig`num') if age<65
by  exp ID: egen Total_H2_combo_orig`num'_65 = total(H2_combo_orig`num') if age<65
by  exp ID: egen Total_H3_combo_orig`num'_65 = total(H3_combo_orig`num') if age<65

}

foreach num of numlist 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14{
by  exp ID: egen Total_H1_combo`num'_65 = total(H1_combo`num') if age<65
by  exp ID: egen Total_H2_combo`num'_65 = total(H2_combo`num') if age<65
by  exp ID: egen Total_H3_combo`num'_65 = total(H3_combo`num') if age<65

}


* create variables with the total number of times individual in poor/fair/good health. 
gen dummy_H1=0 if age<65
replace dummy_H1=1 if H==1 & age<65

gen dummy_H2=0 if age<65
replace dummy_H2=1 if H==2 & age<65

gen dummy_H3=0 if age<65
replace dummy_H3=1 if H==3 & age<65


by  exp ID: egen Total_H1_65 = total(dummy_H1) if age<65
by  exp ID: egen Total_H2_65 = total(dummy_H2) if age<65
by  exp ID: egen Total_H3_65 = total(dummy_H3) if age<65


gen health_65 = H if age==65
bysort exp ID: egen health_65_max = max(health_65)
drop health_65
rename health_65_max health_65
replace health_65=0 if health_65==. // dead

gen R_65 = R if age==65
bysort exp ID: egen R_65_max = max(R_65)
drop R_65
rename R_65_max R_65
replace R_65=0 if R_65==. // dead 

keep if age==25


rename Total_H1_combo0_65 var1
rename Total_H2_combo0_65 var2
rename Total_H3_combo0_65 var3

rename Total_H1_combo_orig0_65 var1_orig
rename Total_H2_combo_orig0_65 var2_orig
rename Total_H3_combo_orig0_65 var3_orig


* for storing results
gen R2_1_1=.
gen R2_2_1=.
gen R2_3_1=.
gen R2_4_1=.
gen R2_5_1=.

gen R2_1_2=.
gen R2_2_2=.
gen R2_3_2=.
gen R2_4_2=.
gen R2_5_2=.

gen R2_1_3=.
gen R2_2_3=.
gen R2_3_3=.
gen R2_4_3=.
gen R2_5_3=.

gen R2_1_4=.
gen R2_2_4=.
gen R2_3_4=.
gen R2_4_4=.
gen R2_5_4=.


**** earnings regressions *************************
*** HS
* IC only
qui reg earnings_PV i.productivity_type##i.latent_health i.H i.R   if  education==1
replace R2_1_1=e(r2_a)

* health,health shocks and IC, but not treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65 if  education==1
replace R2_2_1=e(r2_a)

* health,health shocks, IC and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65 if  education==1
replace R2_3_1=e(r2_a)

* only health and health shocks
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65  if  education==1
replace R2_4_1=e(r2_a)

* only health,health shocks, and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R  age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65  if  education==1
replace R2_5_1=e(r2_a)

* some college
* IC only
qui reg earnings_PV i.productivity_type##i.latent_health i.H i.R   if  education==2
replace R2_1_2=e(r2_a)

* health,health shocks and IC, but not treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65 if  education==2
replace R2_2_2=e(r2_a)

* health,health shocks, IC and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65 if  education==2
replace R2_3_2=e(r2_a)

* only health and health shocks
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65  if  education==2
replace R2_4_2=e(r2_a)

* only health,health shocks, and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R  age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65  if  education==2
replace R2_5_2=e(r2_a)


* college
* IC only
qui reg earnings_PV i.productivity_type##i.latent_health i.H i.R   if  education==3
replace R2_1_3=e(r2_a)

* health,health shocks and IC, but not treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65 if  education==3
replace R2_2_3=e(r2_a)

* health,health shocks, IC and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.productivity_type##i.latent_health i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65 if  education==3
replace R2_3_3=e(r2_a)

* only health and health shocks
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65  if  education==3
replace R2_4_3=e(r2_a)

* only health,health shocks, and treatment
qui reg earnings_PV  Total_H1_65 Total_H2_65  i.H i.R  age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65  if  education==3
replace R2_5_3=e(r2_a)


qui reg earnings_PV i.education##i.productivity_type##i.latent_health i.education##i.H i.education##i.R   
replace R2_1_4=e(r2_a)

* health,health shocks and IC, but not treatment
qui reg earnings_PV i.education##Total_H1_65 i.education##Total_H2_65 i.education##i.productivity_type##i.latent_health i.education##i.H i.education##i.R i.education##age_death_65  i.education##i.health_65 i.education##i.R_65 i.education##age_dp_min_65 i.education##age_du_min_65 i.education##Total_H1_combo_orig*_65 i.education##Total_H2_combo_orig*_65 i.education##Total_H3_combo_orig*_65 
replace R2_2_4=e(r2_a)

* health,health shocks, IC and treatment
qui reg earnings_PV i.education##Total_H1_65 i.education##Total_H2_65 i.education##i.productivity_type##i.latent_health i.education##i.H i.education##i.R i.education##age_death_65  i.education##i.health_65 i.education##i.R_65 i.education##age_dp_min_65 i.education##age_du_min_65 i.education##Total_H1_combo*_65 i.education##Total_H2_combo*_65 i.education##Total_H3_combo*_65 
replace R2_3_4=e(r2_a)

* only health and health shocks
qui reg earnings_PV  Total_H1_65 Total_H2_65 i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo_orig*_65 Total_H2_combo_orig*_65 Total_H3_combo_orig*_65 
replace R2_4_4=e(r2_a)

* only health,health shocks, and treatment
qui reg earnings_PV Total_H1_65 Total_H2_65 i.H i.R age_death_65  i.health_65 i.R_65 age_dp_min_65 age_du_min_65 Total_H1_combo*_65 Total_H2_combo*_65 Total_H3_combo*_65 
replace R2_5_4=e(r2_a)


collapse (mean) R2* 
gen exp=0
reshape long  R2_1_ R2_2_ R2_3_ R2_4_ R2_5_, i(exp) j(education)
rename R2_1_  R2_1
rename R2_2_  R2_2
rename R2_3_  R2_3
rename R2_4_  R2_4
rename R2_5_  R2_5
reshape long  R2_ , i(exp education) j(regression)
reshape wide R2_, i(exp regression) j(education)

drop if regression==2 | regression==4 | regression==5

* Open data and copy paste into Table 106 - all columns and rows are in order 
}

* Table 107 - first 4 columns with PV earnings and CV of PVE
qui{
clear 
use "Experiments Stata Files/Experiment0.dta"
//append using "Experiments Stata Files/Experiment05.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"

replace experiment=1 if experiment==4 // eliminate shocks, lucky, decision rules change


keep if age<=64
gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
keep if age==25

table if experiment==0, stat(fvpercent latent_health)
table education if experiment==0, stat(fvpercent latent_health)

gen I_shock=0
replace I_shock=1 if type2!=1
table education latent_health if experiment==0, stat(mean I_shock)



* by latent type

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ed of numlist 1 2  {
inequal7 earnings_PV if experiment==0 &  latent_health==`ed'
replace COV=r(cov) if experiment==0 & latent_health==`ed'
replace GINI=r(gini) if experiment==0 & latent_health==`ed'
inequal7 earnings_PV if experiment==1 & latent_health==`ed'
replace COV=r(cov) if experiment==1 & latent_health==`ed'
replace GINI=r(gini) if experiment==1 & latent_health==`ed'
}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment latent_health) 
sort latent_health experiment
reshape wide MEAN COV  GINI , i(latent_health) j(experiment)
save "Temp\PV Earnings Variation.dta", replace
restore


* by latent type and education
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ed of numlist 1 2 3 {
foreach prod of numlist 1 2  {
cap inequal7 earnings_PV if experiment==0 &  education==`ed' & latent_health==`prod'
cap replace COV=r(cov) if experiment==0 & education==`ed' & latent_health==`prod'
cap replace MLD=r(mld) if experiment==0 & education==`ed' & latent_health==`prod'
cap replace GINI=r(gini) if experiment==0 & education==`ed' & latent_health==`prod'
cap inequal7 earnings_PV if experiment==1 & education==`ed' & latent_health==`prod'
cap replace COV=r(cov) if experiment==1 & education==`ed' & latent_health==`prod'
cap replace MLD=r(mld) if experiment==1 & education==`ed' & latent_health==`prod'
cap replace GINI=r(gini) if experiment==1 & education==`ed' & latent_health==`prod'
cap inequal7 earnings_PV if experiment==2 & education==`ed' & latent_health==`prod'
cap replace COV=r(cov) if experiment==2 & education==`ed' & latent_health==`prod'
cap replace MLD=r(mld) if experiment==2 & education==`ed' & latent_health==`prod'
cap replace GINI=r(gini) if experiment==2 & education==`ed' & latent_health==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education latent_health) 
sort education experiment latent_health
reshape wide MEAN COV  GINI , i(education latent_health) j(experiment)
append using "Temp\PV Earnings Variation.dta"
save "Temp\PV Earnings Variation.dta", replace


clear 
use "Temp\PV Earnings Variation.dta"
sort education latent_health 
gen order= 1 if 	education==. & latent_health ==2
replace order= 2 if 	education==. & latent_health ==1
replace order= 3 if 	education==1 & latent_health ==2
replace order= 4 if 	education==1 & latent_health ==1
replace order= 5 if 	education==2 & latent_health ==2
replace order= 6 if 	education==2 & latent_health ==1
replace order= 7 if 	education==3 & latent_health ==2
replace order= 8 if 	education==3 & latent_health ==1

sort order

* calculate percentage change from benchmark mean for table
replace MEAN1=(MEAN1-MEAN0)/MEAN0*100

drop GINI* 
order education latent_health MEAN0 MEAN1 COV0 COV1 

* open file here and find the first 4 columns of table in order. 
}

* Table 107 - last 4 columns with Emp rate and SI. 
qui{
clear 
clear 
use "Experiments Stata Files/Experiment0.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"

replace experiment=1 if experiment==4
keep if age<=64

gen I_accept_ESHI =0 if (offer==3 | offer==5 ) & offer_accepted==1
replace I_accept_ESHI =1 if (offer==3 | offer==5 ) & offer_accepted!=1
gen I_accept_no_ESHI=0 if (offer==2 | offer==4 ) & offer_accepted==1
replace I_accept_no_ESHI =1 if (offer==2 | offer==4) & offer_accepted!=1


preserve
collapse (mean) employed_yn TR I_accept_ESHI I_accept_no_ESHI, by(experiment latent_health) 
sort  experiment latent_health
reshape wide employed_yn TR I_accept_ESHI I_accept_no_ESHI, i(latent_health) j(experiment)
save "Temp\LS and SI.dta", replace 
restore


collapse (mean) employed_yn TR I_accept_ESHI I_accept_no_ESHI, by(experiment education latent_health) 
sort  experiment education latent_health
reshape wide employed_yn TR I_accept_ESHI I_accept_no_ESHI, i(education latent_health) j(experiment)
save "Temp\LS and SI 1.dta", replace
clear 
use "Temp\LS and SI.dta"
append using  "Temp\LS and SI 1.dta"

order education latent_health employed_yn0  employed_yn1 TR0 TR1
replace employed_yn0=employed_yn0 * 100
replace employed_yn1=employed_yn1 * 100
replace TR0=TR0*100
replace TR1=TR1*100

sort education latent_health 
gen order= 1 if 	education==. & latent_health ==2
replace order= 2 if 	education==. & latent_health ==1
replace order= 3 if 	education==1 & latent_health ==2
replace order= 4 if 	education==1 & latent_health ==1
replace order= 5 if 	education==2 & latent_health ==2
replace order= 6 if 	education==2 & latent_health ==1
replace order= 7 if 	education==3 & latent_health ==2
replace order= 8 if 	education==3 & latent_health ==1

sort order
drop I_* order

* open data file and copy paste into last 4 columns of table 107

}


* Table 108
qui{
clear 
use "Experiments Stata Files/Experiment0.dta"
append using "Experiments Stata Files/Experiment05.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"

replace experiment=1 if experiment==5 // eliminate shocks, decision rules fixed
replace experiment=2 if experiment==4 // eliminate shocks, lucky, decision rules change

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person
table experiment, stat(mean max_age)


rename education college_alt
sort age college_alt
merge age college_alt using "${mystart}\CPS\CPS Data Files\Intermediate Data Files\Equiv.dta"


rename college_alt education
replace equiv= 1.5 if age>64
replace equiv= 1.5 + (equiv -2.0 )*0.3 if age<65


gen family_consumption = consumption
replace consumption = consumption/ equiv if married==1

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  //1/1.045=0.956937799
gen discounted_consumption=consumption*discounting_factor
bysort experiment ID: egen consumption_PV = total(discounted_consumption)
replace consumption_PV=. if age!=25
replace consumption_PV=consumption_PV/1000

rename consumption_PV earnings_PV

keep if age==25

tabstat earnings_PV if experiment==0, stat(mean sd)

preserve
inequal7 earnings_PV if experiment==0
gen COV=r(cov) if experiment==0
gen GINI=r(gini) if experiment==0
inequal7 earnings_PV if experiment==1
replace COV=r(cov) if experiment==1
replace GINI=r(gini) if experiment==1
inequal7 earnings_PV if experiment==2
replace COV=r(cov) if experiment==2
replace GINI=r(gini) if experiment==2
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI , by(experiment) 
gen education=0
sort education experiment
save "Temp\PV Earnings Variation.dta", replace
restore 

* by education

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ed of numlist 1 2 3 {
inequal7 earnings_PV if experiment==0 &  education==`ed'
replace COV=r(cov) if experiment==0 & education==`ed'
replace GINI=r(gini) if experiment==0 & education==`ed'
inequal7 earnings_PV if experiment==1 & education==`ed'
replace COV=r(cov) if experiment==1 & education==`ed'
replace GINI=r(gini) if experiment==1 & education==`ed'
inequal7 earnings_PV if experiment==2 & education==`ed'
replace COV=r(cov) if experiment==2 & education==`ed'
replace GINI=r(gini) if experiment==2 & education==`ed'
}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education) 
sort education experiment
append using "Temp\PV Earnings Variation.dta"
reshape wide MEAN COV  GINI , i(education) j(experiment)
save "Temp\PV Earnings Variation.dta", replace
restore


*** BY PRODUCTIVITY
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ed of numlist 1 2 3 {
foreach prod of numlist 1 2 3  {
cap inequal7 earnings_PV if experiment==0 &  education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==0 & education==`ed' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==1 & education==`ed' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==2 & education==`ed' & productivity_type==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education productivity_type) 
sort education experiment productivity_type
reshape wide MEAN COV  GINI , i(education productivity_type) j(experiment)
append using "Temp\PV Earnings Variation.dta"
save "Temp\PV Earnings Variation.dta", replace

**** OPEN THS FILE TO COPY PASTE INTO EARNINGS INEQUALITY TABLE
clear 
use "Temp\PV Earnings Variation.dta"
replace productivity_type=0 if productivity_type==.
sort productivity_type education
gen order= 1 if 	education==0 & productivity_type==0
replace order= 2 if 	education==1 & productivity_type==0
replace order= 3 if 	education==2 & productivity_type==0
replace order= 4 if 	education==3 & productivity_type==0
replace order= 5 if 	education==1 & productivity_type==1
replace order= 6 if 	education==1 & productivity_type==2
replace order= 7 if 	education==1 & productivity_type==3
replace order= 8 if 	education==2 & productivity_type==1
replace order= 9 if 	education==2 & productivity_type==2
replace order= 10 if 	education==2 & productivity_type==3
replace order= 11 if 	education==3 & productivity_type==1
replace order= 12 if 	education==3 & productivity_type==2
replace order= 13 if 	education==3 & productivity_type==3
sort order

* calculate percentage change from benchmark mean for table
replace MEAN1=(MEAN1-MEAN0)/MEAN0*100
replace MEAN2=(MEAN2-MEAN0)/MEAN0*100
drop order 

drop if productivity_type>0
drop productivity_type
* open data and copy paste into Table 108 Appendix
}

* Table 109 Appendix
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
append using "Counterfactuals Stata Files/Counterfactual15.dta"
replace experiment = 0 if experiment==16 // benchmark
keep if experiment==0 | experiment==15 

* fraction of each type within educ groups - copy paste into table in column 1
preserve 
keep if  experiment==0 & age==25
collapse (count) ID, by (education productivity_type latent_health)
bysort education: egen total=total(ID)
gen fraction=ID/total*100
restore 

* gen PV utility

* first, need to worry about last period
bysort experiment ID: egen max_age = max(age)
replace Utility = Value_death*0.97 + Utility if age== max_age // add up the utility from dying, discounted
gen time_period = age-25
generate discounting_factor=0.97^(time_period)  // use beta
gen discounted_utility=Utility*discounting_factor
bysort experiment ID: egen Utility_PV = total(discounted_utility)
replace Utility_PV=. if age!=25


replace max_age=max_age+1 //  die at the beginning of next year
replace max_age=. if age>25 

keep if age<65

drop time_period discounting_factor
gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000


**** PREPARE TABLE WITH EMP, SI AND PV EARNINGS, % GOOD H, Utility
gen good_H=1 if H==3
replace good_H=0 if H!=3


replace latent_health=3 if latent_health==1


preserve
collapse (mean) employed_yn TR earnings_PV good_H Utility_PV, by(experiment ) 
sort  experiment 
save "Temp\HI Exp.dta", replace 
restore

preserve
collapse (mean) employed_yn TR earnings_PV good_H Utility_PV, by(experiment education ) 
sort  experiment education 
append using  "Temp\HI Exp.dta"
replace education=0 if education==.
reshape wide employed_yn TR earnings_PV good_H Utility_PV, i(education ) j(experiment)
save "Temp\HI Exp.dta", replace 
restore


collapse (mean)  employed_yn TR earnings_PV good_H Utility_PV, by(experiment education productivity_type latent_health) 
sort  experiment education productivity_type
reshape wide employed_yn TR earnings_PV good_H Utility_PV, i(education productivity_type latent_health) j(experiment)
save "Temp\HI Exp 1.dta", replace
clear 
use "Temp\HI Exp.dta"
append using  "Temp\HI Exp 1.dta"

order education productivity_type employed_yn0  employed_yn15 TR0 TR15 earnings_PV0 earnings_PV15 good_H0 good_H15 Utility_PV0  Utility_PV15

foreach var of varlist employed_yn0  employed_yn15 TR0  TR15  good_H0  good_H15 {
replace `var'=`var' * 100
}

gen PV_earnings_change15 = (earnings_PV15 - earnings_PV0)/earnings_PV0 * 100

replace employed_yn15= employed_yn15-employed_yn0
replace TR15= TR15-TR0
replace good_H15=good_H15-good_H0

drop earnings_PV15 
rename PV_earnings_change15  earnings_PV15
gen UTI_change15 = (Utility_PV15 - Utility_PV0)/(-Utility_PV0)*100
drop Utility_PV15  Utility_PV0 

order education productivity_type latent_health employed_yn0  employed_yn15 TR0 TR15 earnings_PV0 earnings_PV15 good_H0 good_H15 UTI_change15
* opend data and copy paste into table - all columns and rows are in order
}

* Table 110 Appendix

qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
append using "Counterfactuals Stata Files/Counterfactual17.dta"
replace experiment = 0 if experiment==16 // benchmark
keep if experiment==0 | experiment==17 

* fraction of each type within educ groups - copy paste into table in column 1
preserve 
keep if  experiment==0 & age==25
collapse (count) ID, by (education productivity_type latent_health)
bysort education: egen total=total(ID)
gen fraction=ID/total*100
restore 

* gen PV utility

* first, need to worry about last period
bysort experiment ID: egen max_age = max(age)
replace Utility = Value_death*0.97 + Utility if age== max_age // add up the utility from dying, discounted
gen time_period = age-25
generate discounting_factor=0.97^(time_period)  // use beta
gen discounted_utility=Utility*discounting_factor
bysort experiment ID: egen Utility_PV = total(discounted_utility)
replace Utility_PV=. if age!=25


replace max_age=max_age+1 //  die at the beginning of next year
replace max_age=. if age>25 

keep if age<65

drop time_period discounting_factor
gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000


**** PREPARE TABLE WITH EMP, SI AND PV EARNINGS, % GOOD H, Utility
gen good_H=1 if H==3
replace good_H=0 if H!=3


replace latent_health=3 if latent_health==1


preserve
collapse (mean) employed_yn TR earnings_PV good_H Utility_PV, by(experiment ) 
sort  experiment 
save "Temp\HI Exp.dta", replace 
restore

preserve
collapse (mean) employed_yn TR earnings_PV good_H Utility_PV, by(experiment education ) 
sort  experiment education 
append using  "Temp\HI Exp.dta"
replace education=0 if education==.
reshape wide employed_yn TR earnings_PV good_H Utility_PV, i(education ) j(experiment)
save "Temp\HI Exp.dta", replace 
restore


collapse (mean)  employed_yn TR earnings_PV good_H Utility_PV, by(experiment education productivity_type latent_health) 
sort  experiment education productivity_type
reshape wide employed_yn TR earnings_PV good_H Utility_PV, i(education productivity_type latent_health) j(experiment)
save "Temp\HI Exp 1.dta", replace
clear 
use "Temp\HI Exp.dta"
append using  "Temp\HI Exp 1.dta"

order education productivity_type employed_yn0  employed_yn17 TR0 TR17 earnings_PV0 earnings_PV17 good_H0 good_H17 Utility_PV0  Utility_PV17

foreach var of varlist employed_yn0  employed_yn17 TR0  TR17  good_H0  good_H17 {
replace `var'=`var' * 100
}

gen PV_earnings_change17 = (earnings_PV17 - earnings_PV0)/earnings_PV0 * 100

replace employed_yn17= employed_yn17-employed_yn0
replace TR17= TR17-TR0
replace good_H17=good_H17-good_H0

drop earnings_PV17 
rename PV_earnings_change17  earnings_PV17
gen UTI_change17 = (Utility_PV17 - Utility_PV0)/(-Utility_PV0)*100
drop Utility_PV17  Utility_PV0 

order education productivity_type latent_health employed_yn0  employed_yn17 TR0 TR17 earnings_PV0 earnings_PV17 good_H0 good_H17 UTI_change17
* opend data and copy paste into table - all columns and rows are in order
}


* Table 14 Appendix
* frequency of shocks 
qui{
clear
use "Experiments Stata Files/Experiment0.dta"

gen I_d = 0 
replace I_d=1 if du==1 | dp==1
gen I_any_shock=0
replace I_any_shock=1 if du==1 | dp==1 | s==1
gen I_major_shock=0
sort ID age
replace I_major_shock=1 if (du==1 | dp==1) & H[_n+1]<H & ID==ID[_n+1]

keep if age<65

table (education productivity_type), stat(mean I_d I_major_shock)

collapse (mean) I_any_shock  I_d  dp I_major_shock, by(education)
sort education
save "Temp\Schocks_freq.dta", replace

clear
use "Experiments Stata Files/Experiment0.dta"
gen I_major_shock=0
sort ID age
replace I_major_shock=1 if (du==1 | dp==1) & H[_n+1]<H & ID==ID[_n+1]

gen I_d = 0 
replace I_d=1 if du==1 | dp==1
gen I_any_shock=0
replace I_any_shock=1 if du==1 | dp==1 | s==1
keep if age==30 | age==55
collapse (mean) I_any_shock  I_d  dp I_major_shock , by(education age)
reshape wide  I_any_shock  I_d  dp I_major_shock, i(education) j(age)
sort education
merge education using "Temp\Schocks_freq.dta"

drop _merge
rename I_any_shock  I_any_shock0
rename I_d I_d0
rename dp dp0
rename I_major_shock I_major_shock0

reshape long  I_any_shock  I_d  dp I_major_shock, i(education ) j(jage)
replace I_any_shock=I_any_shock*100
replace  I_d = I_d*100
replace  dp = dp*100
replace  I_major_shock = I_major_shock*100
save "Temp\Schocks_freq.dta", replace


*** all educ groups

clear
use "Experiments Stata Files/Experiment0.dta"

gen I_d = 0 
replace I_d=1 if du==1 | dp==1
gen I_any_shock=0
replace I_any_shock=1 if du==1 | dp==1 | s==1
gen I_major_shock=0
sort ID age
replace I_major_shock=1 if (du==1 | dp==1) & H[_n+1]<H & ID==ID[_n+1]

keep if age<65

collapse (mean) I_any_shock  I_d  dp I_major_shock
save "Temp\Schocks_freq 1.dta", replace

clear
use "Experiments Stata Files/Experiment0.dta"
gen I_major_shock=0
sort ID age
replace I_major_shock=1 if (du==1 | dp==1) & H[_n+1]<H & ID==ID[_n+1]

gen I_d = 0 
replace I_d=1 if du==1 | dp==1
gen I_any_shock=0
replace I_any_shock=1 if du==1 | dp==1 | s==1
keep if age==30 | age==55
collapse (mean) I_any_shock  I_d  dp I_major_shock , by( age)
append  using "Temp\Schocks_freq 1.dta"

rename I_any_shock  I_any_shock0
rename I_d I_d0
rename dp dp0
rename I_major_shock I_major_shock0

replace age=0 if age==.
sort age

replace I_any_shock0=I_any_shock0*100
replace  I_d0 = I_d0*100
replace  dp0 = dp0*100
replace  I_major_shock0 = I_major_shock0*100
rename I_any_shock0 I_any_shock
rename I_major_shock0 I_major_shock
rename I_d0 I_d
rename dp0 dp
rename age jage

append using "Temp\Schocks_freq.dta"
replace jage=25 if jage ==0
* OPEN data - copy paste into table 14 Appendix. all numbers in order
}


* Figure 12
qui{
*** OOP distribution - model and data
clear 
use "Experiments Stata Files/Experiment0.dta"
keep if age<65
replace OOP=0 if I_treat == 0 | I_pay == 0
replace OOP= 0 if tr_pay_alt==2 
drop  if tr_pay_alt==2

egen OOP_1 = pctile(OOP), p(99)
gen OOP_1_use = OOP if OOP>=OOP_1
egen OOP_1_total = total(OOP_1_use) 

egen OOP_5 = pctile(OOP), p(95)
gen OOP_5_use = OOP if OOP>=OOP_5
egen OOP_5_total = total(OOP_5_use) 

egen OOP_10 = pctile(OOP), p(90)
gen OOP_10_use = OOP if OOP>=OOP_10
egen OOP_10_total = total(OOP_10_use) 

egen OOP_15 = pctile(OOP), p(85)
gen OOP_15_use = OOP if OOP>=OOP_15
egen OOP_15_total = total(OOP_15_use) 

egen OOP_20 = pctile(OOP), p(80)
gen OOP_20_use = OOP if OOP>=OOP_20
egen OOP_20_total = total(OOP_20_use) 

egen OOP_50 = pctile(OOP), p(50)
gen OOP_50_use = OOP if OOP>=OOP_50
egen OOP_50_total = total(OOP_50_use) 

egen total_OOP=total(OOP)
replace OOP_1_total=OOP_1_total/total_OOP * 100
replace OOP_5_total=OOP_5_total/total_OOP* 100
replace OOP_10_total=OOP_10_total/total_OOP* 100
replace OOP_15_total=OOP_15_total/total_OOP* 100
replace OOP_20_total=OOP_20_total/total_OOP* 100
replace OOP_50_total=OOP_50_total/total_OOP* 100
table, stat(mean OOP_1_total OOP_5_total OOP_10_total OOP_15_total OOP_20_total OOP_50_total)

keep OOP_1_total OOP_5_total OOP_10_total OOP_15_total OOP_20_total OOP_50_total
rename OOP_1_total OOP_1
rename OOP_5_total OOP_5
rename OOP_10_total OOP_10
rename OOP_15_total OOP_15
rename OOP_20_total OOP_20
rename OOP_50_total OOP_50

collapse (mean) OOP_1 OOP_5 OOP_10 OOP_15 OOP_20 OOP_50
gen Data=.
reshape long OOP_, i(Data) j(percent)
drop Data
rename OOP_ OOP_Model
sort percent
merge percent using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\OOP.dta"

sort percent

#delimit ;
graph bar OOP_Model OOP_MEPS, over(percent, relabel(1 "Top 1%" 2 "Top 5%" 3 "Top 10%" 4 "Top 15%" 5 "Top 20%" 6 "Top 50%"))
  ytitle("Percent of Total OOP Spending")
  title("Concentration of OOP among non-Medicaid individuals", color(black) size(medium))
  legend( label(1 "Model")  label(2 "Data"))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\OOP_distribution.eps", replace
}